package com.kerbores.meeting.module;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.imageio.ImageIO;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.random.R;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.ChainBy;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.annotation.Views;
import org.nutz.mvc.impl.NutActionChainMaker;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

import com.kerbores.meeting.dao.IDeviceDao;
import com.kerbores.meeting.domain.Device;
import com.kerbores.meeting.domain.Device.DeviceType;
import com.kerbores.meeting.domain.User;
import com.kerbores.meeting.module.base.BaseModule;
import com.kerbores.nutz.module.base.NameSpace;
import com.kerbores.nutz.setup.PermissionSetup;
import com.kerbores.nutz.view.velocity.VelocityViewMaker;
import com.kerbores.utils.db.file.DBFile;
import com.kerbores.utils.entries.Result;

/**
 * 主模块
 * 
 * @author Kerbores
 *
 *         Create At 2015年1月8日 下午1:57:34
 */
@Modules(scanPackage = true)
@IocBy(type = ComboIocProvider.class, args = { "*org.nutz.ioc.loader.json.JsonLoader", "config", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "com.kerbores" })
@Ok("json")
@Views({ VelocityViewMaker.class })
@SetupBy(PermissionSetup.class)
@ChainBy(type = NutActionChainMaker.class, args = { "chain/chains.js" })
@NameSpace("main")
@Fail("http:500")
public class MainModule extends BaseModule {
	@Inject
	private Dao dao;
	@Inject
	private IDeviceDao deviceDao;

	@At("/test")
	@Ok("vel:test/test.vm")
	public Result test() throws UnsupportedEncodingException {
		deviceDao.createTable();
		Device device = new Device();
		device.setDeviceType(DeviceType.from(R.random(0, DeviceType.values().length)));
		device.setUid(R.UU16());
		deviceDao.save(device);
		_addCookie("test", URLEncoder.encode("我是中文", "UTF-8"), 1000);
		return Result.success().addData("dao", dao.meta()).addData("devices", deviceDao.findAll());
	}

	@At("404")
	@Ok("jsp:404")
	public Result _404() {
		return Result.success();
	}

	@At("500")
	@Ok("jsp:500")
	public Result _500() {
		throw new RuntimeException("500");
	}

	@At("/validation")
	public Result validation(@Param("..") User user, long tt) {
		return Result.success().addData("user", user);
	}

	@At("/downloadById/?")
	@Ok("raw")
	public File downloadById(int id) throws IOException {
		DBFile file = dao.fetch(DBFile.class, Cnd.where("id", "=", id));
		if (file == null) {
			return null;
		}
		File f = file.getFile();
		return f;
	}

	@At("/downloadByUUID/?")
	@Ok("raw")
	public File downloadByUUID(String uuid) throws IOException {
		DBFile file = dao.fetch(DBFile.class, Cnd.where("uuid", "=", uuid));
		if (file == null) {
			return null;
		}
		File f = file.getFile();
		return f;
	}

	/**
	 * 显示图片
	 * 
	 * @param id
	 *            图片编号
	 * @throws IOException
	 */
	@At("/getImageById/?")
	@Ok("raw")
	public void getImageById(int id) throws IOException {
		DBFile file = dao.fetch(DBFile.class, Cnd.where("id", "=", id));
		if (file == null || !file.isImg()) {
			Mvcs.getResp().getWriter().write("BAD REQUEST!");
			return;
		}
		File f = file.getFile();
		ImageIO.write(ImageIO.read(f), file.getType(), Mvcs.getResp().getOutputStream());
	}

	/**
	 * 显示图片
	 * 
	 * @param uuid
	 *            图片uuid
	 * @throws IOException
	 */
	@At("/getImageByUUID/?")
	@Ok("raw")
	public void getImageByUUID(String uuid) throws IOException {
		DBFile file = dao.fetch(DBFile.class, Cnd.where("uuid", "=", uuid));
		if (file == null || !file.isImg()) {
			Mvcs.getResp().getWriter().write("BAD REQUEST!");
			return;
		}
		File f = file.getFile();
		ImageIO.write(ImageIO.read(f), file.getType(), Mvcs.getResp().getOutputStream());
	}

	/* (non-Javadoc)
	 * @see com.kerbores.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "main";
	}
}
